package com.lanchetech.admin.controller;

import com.lanchetech.admin.aop.OperatorLog;
import com.lanchetech.admin.service.ArticleService;
import com.lanchetech.bean.request.PageReq;
import com.lanchetech.bean.response.BasePageResp;
import com.lanchetech.bean.response.BaseResp;
import com.lanchetech.bean.response.ResultData;
import com.lanchetech.entity.Article;
import com.lanchetech.entity.CompanyIntroduction;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Api(value = "文章管理接口", tags = "文章管理接口")
@RequestMapping("/api/article")
@RestController
public class ArticleController {

    @Autowired
    ArticleService articleService;

    @ApiOperation("获取文章详细信息")
    @PreAuthorize("hasAuthority('web.article-edit')")
    @GetMapping("/getArticleDetail")
    public ResultData<Article> getArticleDetail(@RequestParam("id") Long id) {
        return articleService.getArticleDetail(id);
    }

    @OperatorLog("创建和编辑文章")
    @ApiOperation("创建和编辑文章")
    @PreAuthorize("hasAuthority('web.article-index.edit.auth')")
    @PostMapping("/editArticle")
    public BaseResp editArticle(@RequestBody Article article) {
        return articleService.editArticle(article);
    }

    @ApiOperation("获取所有文章列表")
    @PreAuthorize("hasAuthority('web.article-index')")
    @PostMapping("/getArticlePage")
    public ResultData<BasePageResp<Article>> getArticlePage(@RequestBody PageReq req) {
        return articleService.getArticlePage(req);
    }

    @OperatorLog("删除文章")
    @ApiOperation("删除文章")
    @PreAuthorize("hasAuthority('web.article-index.edit.auth')")
    @GetMapping("/deleteArticle")
    public BaseResp deleteArticle(@RequestParam("id") Long id) {
        return articleService.deleteArticle(id);
    }

    @OperatorLog("设置文章作用")
    @ApiOperation("设置文章作用")
    @PreAuthorize("hasAuthority('web.article-index.edit.auth')")
    @GetMapping("/editArticleEffect")
    public BaseResp editArticleEffect(@RequestParam("id") Long id,@RequestParam(value = "effect" ,required = false) String effect) {
        return articleService.editArticleEffect(id,effect);
    }

    @OperatorLog("编辑公司简介")
    @ApiOperation("编辑公司简介")
    @PostMapping("/editCompanyIntroduction")
    public BaseResp editCompanyIntroduction(@RequestBody CompanyIntroduction req) {
        return articleService.editCompanyIntroduction(req);
    }

    @ApiOperation("获取公司简介")
    @GetMapping("/getCompanyIntroduction")
    public ResultData<CompanyIntroduction> getCompanyIntroduction() {
        return articleService.getCompanyIntroduction();
    }
}
